/*********************************************************************/
/*describe:本程序完成从串口的数据读写，并保存到memfifo中 */
/*author:DocRun                         */
/* Thu May 14 08:12:33 2009   copyright-by ruanbanyong@gmail.com*/
#include "serial.h"
#include "gprs.h"

#define TRUE   1 
#define FALSE  0
#define MSGSIZE 4 //初始消息数据为4个字节


int main()	
{
	int fd;
	int fd_memfifo;
	int retr,retw;
	int msg[MSGSIZE];
	
	char ch;

	char *dev ="/dev/ttyS0";  
	//打开mmefifo如果失败则退出(memefifo中保存了采集的汽车信息
	fd_memfifo = open("/dev/memfifo",O_RDWR);
	if(fd == -1)
	{
	     printf("open /dev/memfifo failed");
	     return -1;
	 
	}
    
	fd = init_serial(dev);
	if (fd>0)
		set_speed(fd,115200);
	else	{
		printf("Can't Open Serial Port!\n");
		exit(0);
	}
	if (set_parity(fd,8,1,'N')== FALSE)	{
		printf("Set Parity Error\n");
		exit(1);
	}
	printf("successfully set serial \n");
	
	//init gprs
	if (GprsInit(fd) == -1)
	{
	     printf("gprs init failed \n");
	     exit(1);
	}

	
	while(1)	
	{
	     

	     retr =  GprsReadData(fd,(void*)msg,sizeof(int)*MSGSIZE); //读取数据
	     if(retr != sizeof(int)*MSGSIZE)
	     {
		  printf("read failed \n");
		  continue;
		  
	     }
	     else
	     {
		  //将数据写入共享缓存
		  retw = write(fd_memfifo,(void*)msg,sizeof(int)*MSGSIZE);
		  if(retw != sizeof(int)*MSGSIZE)
		  {
		       printf("write failed\n");
		       
		  }
		  
	     }
	     
	     
	}
//	close(fd);
	return(0);
}
